package com.shunhe.zs_pda.ui.sys.p

import com.shunhe.oa_web.net.http.HttpRes
import com.shunhe.zs_pda.base.P
import com.shunhe.zs_pda.model.User
import com.shunhe.zs_pda.ui.sys.LoginActivity
import com.shunhe.zs_pda.util.http.HttpApi
import com.shunhe.zs_pda.util.http.HttpApi.wait
import com.shunhe.zs_pda.util.toast
import kotlinx.coroutines.launch

class LoginP(val a: LoginActivity) : P() {

    // 登录
    fun reqLogin(account: String, password: String) {
        a.hideProgress()
        a.showProgress()
        HttpApi.io.launch {
            try {
                val res: HttpRes<User> = HttpApi.api.login(account, password).wait()
                log("reqLogin.code=${res.code},msg=${res.msg},data=${res.data}")
                ui(a) {
                    a.hideProgress()
                    if (res.code == "1") {
                        val data = res.data
                        if (null != data) {
                            a.onRes(1, data)
                        }
                    } else {
                        (res.msg ?: "登录异常").toast()
                    }
                }
            } catch (e: Exception) {
                log("reqLogin.数据异常.${e.cause}.${e.message}")
                ui(a) {
                    a.hideProgress()
                    "数据异常".toast()
                }
            }
        }
    }

}